package com.nanychuan.blogback.service.impl;

import cn.hutool.core.date.DateUnit;
import com.nanychuan.blogback.mapper.ArticleMapper;
import com.nanychuan.blogback.mapper.CategoryMapper;
import com.nanychuan.blogback.mapper.TagMapper;
import com.nanychuan.blogback.pojo.Article;
import com.nanychuan.blogback.pojo.Category;
import com.nanychuan.blogback.pojo.Tag;
import com.nanychuan.blogback.service.ArticleService;
import com.nanychuan.blogbase.exception.BlogEnums;
import com.nanychuan.blogbase.exception.BlogException;
import com.nanychuan.blogbase.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author 往
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private TagMapper tagMapper;

    @Override
    public List<Article> selectArticle(String uid, String title) {
        //搜索框模糊查询
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uid", uid);
        if (title != null && !title.equals("")) {
            criteria.andLike("title", "%" + title + "%");
        }
        //查询全部文章
        List<Article> articleList = articleMapper.selectByExample(example);

        if (articleList.size() != 0 && articleList != null) {
            //遍历文章集合
            for (Article article : articleList) {
                //获取cid
                String cid = article.getCid();
                Category category = categoryMapper.selectByPrimaryKey(cid);
                article.setCid(category.getCname());
            }
        }
        return articleList;
    }

    @Override
    public void isOpen(Article article) {
        //调用tkMapper更新方法
        int count = articleMapper.updateByPrimaryKeySelective(article);
        if (count == 0) {
            throw new BlogException(BlogEnums.USER_UPDATE_INFORMATION);
        }
    }

    @Override
    public List<Category> queryCategory() {
        return categoryMapper.selectAll();
    }

    @Override
    public List<Tag> queryTags(String cid) {
        Tag tag = new Tag();
        tag.setCid(cid);
        List<Tag> tagList = tagMapper.select(tag);
        return tagList;
    }

    //异步发布文章
    @Override
    public Article addArticle(Article article) {
        //设置文章特殊字段默认值（后期可以在数据库进行设置）
        article.setThumbsUp("0");//点赞数
        article.setIs_hot("0");//是否热门
        article.setVisit_count("0");//访问量
        article.setCreate_time(DateTimeUtil.getSysTime());//发布时间
        article.setIsCommented("0");//是否被评论
        //添加数据
        int count = articleMapper.insertSelective(article);
        if (count == 0) {
            //添加失败
            throw new BlogException(BlogEnums.ARTICLE_PUNISH);
        }
        return article;
    }

    @Override
    public Article queryById(String id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    //修改文章信息
    @Override
    public Article updateArticle(Article article) {
        //设置文章特殊字段默认值（后期可以在数据库进行设置）
        article.setThumbsUp("0");//点赞数
        article.setIs_hot("0");//是否热门
        article.setVisit_count("0");//访问量
        article.setUpdate_time(DateTimeUtil.getSysTime());//更新时间
        article.setIsCommented("0");//是否被评论
        //修改数据
        int count = articleMapper.updateByPrimaryKeySelective(article);
        if (count == 0) {
            //添加失败
            throw new BlogException(BlogEnums.ARTICLE_UPDATE);
        }
        return article;
    }

    @Override
    public void deleteArticle(String id) {
        int count = articleMapper.deleteByPrimaryKey(id);
        if (count==0){
            throw new BlogException(BlogEnums.ARTICLE_DELETE);
        }
    }
}
